package com.springcloud.auth.modular.auth.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springcloud.auth.entity.TToken;
import com.springcloud.auth.entity.TUserInfo;
import com.springcloud.auth.mapper.TUserInfoMapper;
import com.springcloud.auth.modular.auth.service.LoginService;
import com.springcloud.auth.service.TTokenService;
import com.springcloud.auth.utils.GenerateUtil;
import com.springcloud.auth.utils.OauthUtils;
import com.springcloud.auth.utils.TokenUtil;
import com.springcloud.auth.utils.UserManageUtil;
import com.springcloud.auth.vo.TokenVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Resource
    private TUserInfoMapper userMapper;

    @Resource
    private TTokenService tokenService;

    @Override
    public TUserInfo login(String userName, String password) {
        String salt = GenerateUtil.getSalt(userName);
        String decodePwd = GenerateUtil.genePwd(password, salt);

        QueryWrapper<TUserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TUserInfo::getUserName,userName);
        queryWrapper.lambda().eq(TUserInfo::getPassword,decodePwd);

        TUserInfo info = userMapper.selectOne(queryWrapper);

        return info;
    }

    @Override
    public Map<String, Object> saveUserInfo(TUserInfo info, TokenVo tokenVo) throws Exception {
        // 生成TonkenEntity
        String token = TokenUtil.createToken(info.getId(), tokenVo.getAcToken(), tokenVo.getReToken());

        //存入redis
        OauthUtils.saveRedisToken(tokenVo.getAcToken(), tokenVo.getReToken(), token);

        // 存入数据库
        Date now = new Date();

        TToken tokenInfo = new TToken();
        tokenInfo.setAccessToken(tokenVo.getAcToken());
        tokenInfo.setRefreshToken(tokenVo.getReToken());
        tokenInfo.setUserId(info.getId());
        tokenInfo.setToken(token);

        tokenInfo.setCreateTime(now);
        tokenInfo.setAccessUpdateTime(now);
        tokenInfo.setRefreshUpdateTime(now);

        tokenService.save(tokenInfo);

        Map<String, Object> map = new HashMap<>();
        map.put("acToken", tokenVo.getAcToken());
        map.put("reToken", tokenVo.getReToken());

        //查看该用户是否登录过
        boolean isLogin = UserManageUtil.has(info.getId());

        if (isLogin) {
            //踢出旧用户,清除旧token
            UserManageUtil.removeTokenRedis(info.getId());
        }
        //记录用户数
        UserManageUtil.setToken(info.getId(), tokenVo);

        return map;
    }

    @Override
    public void loginOut() {
        TUserInfo userInfo = TokenUtil.getUserInfo();

        //清除旧token
        UserManageUtil.removeTokenRedis(userInfo.getId());

        //踢出旧用户
        TokenVo oldTokenVo = UserManageUtil.getToken(userInfo.getId());

        //清理redis
        tokenService.clearToken(oldTokenVo.getAcToken());
    }

}
